<?php
namespace app\api\controller;

use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Db;
use app\api\controller\Adv;
use app\api\model\User as ModelUser;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;

class Shop extends ApiController
{
    protected $noNeedLogin = [
        "get_banner_list",
        "get_classification_list",
        "recommend_service"
        
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    //轮播图列表
    public function get_banner_list()
    {
        $param = $this->request->param();
        $num = isset($param['num'])?$param['num']:5;
        $list = Db::name("banner")->where("type",$param["type"])->order('id desc')
            ->paginate($num);
        $this->success('获取成功', $list);
    }
 
   
    //分类列表
    public function get_classification_list(){
        $param = $this->request->param();
        if(empty($param["type"])){
            $this->error("类型不能为空");
        }
        if($param["type"] == 1){
            $list = Db::name("classification")->where("is_recommend",1)->order('sort asc')->paginate(8);
        }else{
            $list = Db::name("classification")->order('sort asc')->paginate(10);
        }
        $list = $list->toArray();
        $this->success('获取成功', $list);
    }

    //精选推荐
    public function recommend_service(){
        $list = Db::name("service_goods")->where("is_recommend",1)
            ->order('create_time desc')
            ->field("id,title,zhu_img,server_label")
            ->paginate(10)->each(function($item){
                $service_duration = Db::name("service_duration")->where("service_gid",$item["id"])->order("id asc")->find();
                if(!empty($service_duration)){
                    $item["spec_price"] = $service_duration["price"];
                }else{
                    $item["spec_price"] = "";
                }
                //好评率
                $zong_num = Db::name("order_evaluate")->where("service_good_id",$item["id"])->count();
                $order_evaluate = Db::name("order_evaluate")->where("service_good_id",$item["id"])->where("star_rating","in","4,5")->count();
                if($zong_num == 0 || $order_evaluate== 0){
                    $item["plsz"] =0;
                }else{
                    $plsz = bcdiv($order_evaluate,$zong_num,2);
                    $plsz = intval($plsz * 100);
                    $item["plsz"] =$plsz;
                }
                //已售 
                $sold = Db::name("order")->where("service_gid",$item["id"])->where("status",7)->count();
                $item["sold"] =$sold;
                $item["server_label"] = explode(",",$item["server_label"]);
                return $item;
            });
        $list = $list->toArray();
        $this->success('获取成功', $list);
    }
 
    //分类产品列表
    public function all_class_list(){
        $param = $this->request->param();
        if(empty($param["cllas_id"])){
            $this->error("分类id不能为空");
        }
        $list = Db::name("service_goods")->where("service_class",$param["cllas_id"])
            ->order('create_time desc')
            ->field("id,title,zhu_img,server_label")
            ->paginate(10)->each(function($item){
                $service_duration = Db::name("service_duration")->where("service_gid",$item["id"])->order("id asc")->find();
                if(!empty($service_duration)){
                    $item["spec_price"] = $service_duration["price"];
                }else{
                    $item["spec_price"] = "";
                }
                //好评率
                $zong_num = Db::name("order_evaluate")->where("service_good_id",$item["id"])->count();
                $order_evaluate = Db::name("order_evaluate")->where("service_good_id",$item["id"])->where("star_rating","in","4,5")->count();
                if($zong_num == 0 || $order_evaluate== 0){
                    $item["plsz"] =0;
                }else{
                    $plsz = bcdiv($order_evaluate,$zong_num,2);
                    $plsz = intval($plsz * 100);
                    $item["plsz"] =$plsz;
                }
                //已售 
                $sold = Db::name("order")->where("service_gid",$item["id"])->where("status",7)->count();
                $item["sold"] =$sold;
                $item["server_label"] = explode(",",$item["server_label"]);
                return $item;
            });
        $list = $list->toArray();
        $this->success('获取成功', $list);
    }

    //服务产品详情
    public function service_detail(){
        $param = $this->request->param();
        if(empty($param["sid"])){
            $this->error("服务产品id不能为空");
        }
        
        $list = Db::name("service_goods")->where("id",$param['sid'])->find();
        if(empty($list)){
            $this->error("服务不存在");
        }

        $service_duration = Db::name("service_duration")->where("service_gid",$param["sid"])->order("id asc")->find();
        if(!empty($service_duration)){
            $list["price"] = $service_duration["price"];
        }else{
            $list["price"] = "";
        }
         //好评率
         $zong_num = Db::name("order_evaluate")->where("service_good_id",$list["id"])->count();
         $order_evaluate = Db::name("order_evaluate")->where("service_good_id",$list["id"])->where("star_rating","in","4,5")->count();
         
        if($zong_num == 0 || $order_evaluate== 0){
            $list["plsz"] =0;
        }else{
            $plsz = bcdiv($order_evaluate,$zong_num,2);
            $plsz = intval($plsz * 100);
            $list["plsz"] =$plsz;
        }

         //评价
         $good_coten = Db::name("order_evaluate")->where("service_good_id",$param["sid"])
                ->where("status",0)
                ->paginate(10)->each(function($item){
                    $user = Db::name("user")->where("id",$item["uid"])->find();
                    $item["nickname"] = $user["nickname"];
                    $item["avatar"] = $user["avatar"];
                    return $item;
                });
         $my_collection = Db::name("my_collection")->where("gid",$param["sid"])->where("uid",$this->uid)->find();
         if(empty($my_collection)){
            //未收藏
            $is_my_collection = 1;
         }else{
            //已收藏
            $is_my_collection = 2;
         }

         //已售 
        $sold = Db::name("order")->where("service_gid",$list["id"])->where("status",7)->count();
        $list["is_my_collection"] =$is_my_collection;
        $list["sold"] =$sold;
        $list["server_label"] = explode(",",$list["server_label"]);
        $list['lun_img'] = explode("|",$list['lun_img']);
        $list['desc'] = html_entity_decode($list['desc']);
        $list['desc'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $list['desc']);
        $list['spec'] = json_decode($list["sku_attribute_list"],true);
        $list["good_coten"] =$good_coten;
        $this->success('获取成功', $list);
    }

    //服务产品搜索
    public function serive_good_resah(){
        $param = $this->request->param();
        if (empty($param['title'])) {
            $this->error('参数传递错误!');
        }
        $where = [];
        if(!empty($param['title']) && isset($param['title']))
        {
            $where = [
                ['title', 'like', '%'.$param['title'].'%']
            ];
        }
        $list = Db::name("service_goods")->where($where)
        ->order('create_time desc')
        ->field("id,title,zhu_img,server_label")
        ->paginate(10)->each(function($item){
            $service_duration = Db::name("service_duration")->where("service_gid",$item["id"])->order("id asc")->find();
            if(!empty($service_duration)){
                $item["spec_price"] = $service_duration["price"];
            }else{
                $item["spec_price"] = "";
            }
            //好评率
            $zong_num = Db::name("order_evaluate")->where("service_good_id",$item["id"])->count();
            $order_evaluate = Db::name("order_evaluate")->where("service_good_id",$item["id"])->where("star_rating","in","4,5")->count();
            if($zong_num == 0 || $order_evaluate== 0){
                $item["plsz"] =0;
            }else{
                $plsz = bcdiv($order_evaluate,$zong_num,2);
                $plsz = intval($plsz * 100);
                $item["plsz"] =$plsz;
            }
            //已售 
            $sold = Db::name("order")->where("service_gid",$item["id"])->where("status",7)->count();
            $item["sold"] =$sold;
            $item["server_label"] = explode(",",$item["server_label"]);
            return $item;
        });
        $list = $list->toArray();
        $this->success('获取成功', $list);
    }

    //选择规格
    public function select_specifications(){
        $param = $this->request->param();
        if(empty($param["gid"])){
            $this->error("商品id不能为空");
        }
        if(empty($param["spec_value"])){
            $this->error("规格不能为空");
        }
       // dump($param["spec_value"]);die;
       // $spec_value = ["服务人数"=> "1人","服务时长" =>"2小时"];
        $spec_value = json_encode($param["spec_value"],true); 

        $flag = Db::name("service_duration")->where("service_gid",$param["gid"])->where("specs_list",$spec_value)->field("id,price,stock")->find();
        if(empty($flag)){
            $this->error("规格不存在");
        }
        if($flag["stock"] <= 0){
            $this->error("库存不足");
        }
        $this->success('获取成功', $flag);
       
    }

     //我的收藏列表
     public function collection_list(){
        $list = Db::name("my_collection")->where("uid",$this->uid)
        ->paginate(10)->each(function($item){
            $service_goods = Db::name("service_goods")->where("id",$item["gid"])->field("id,title,zhu_img,server_label")->find();
            $service_duration = Db::name("service_duration")->where("service_gid",$item["gid"])->order("id asc")->find();
            if(!empty($service_duration)){
                $item["spec_price"] = $service_duration["price"];
            }else{
                $item["spec_price"] = "";
            }
            //好评率
            $zong_num = Db::name("order_evaluate")->where("service_good_id",$item["id"])->count();
            $order_evaluate = Db::name("order_evaluate")->where("service_good_id",$item["id"])->where("star_rating","in","4,5")->count();
            if($zong_num == 0 || $order_evaluate== 0){
                $item["plsz"] =0;
            }else{
                $plsz = bcdiv($order_evaluate,$zong_num,2);
                $plsz = intval($plsz * 100);
                $item["plsz"] =$plsz;
            }
            //已售 
            $sold = Db::name("order")->where("service_gid",$item["gid"])->where("status",7)->count();
            $item["sold"] =$sold;
            $item["server_label"] = explode(",",$service_goods["server_label"]);
            $item["service_goods"] =$service_goods;
            return $item;
        });
        $list = $list->toArray();
        $this->success('获取成功', $list);
     }

     //添加收藏
     public function add_collection(){
        $param = $this->request->param();
        if(empty($param['gid'])){
            $this->error('未找到指定产品');
        }
        Db::name("my_collection")->insert(["uid"=>$this->uid,"gid"=>$param['gid'],"creat_time"=>time()]);
        $this->success('添加成功');
    }

    //取消收藏
    public function cancel_collection(){
        $param = $this->request->param();
        if(empty($param['gid'])){
            $this->error('未找到指定产品');
        }
        Db::name("my_collection")->where("gid",$param['gid'])->where("uid",$this->uid)->delete();
        $this->success('取消成功');
    }

    //我的地址列表
    public function myaddress_list(){
        $list = Db::name("address")->where("uid",$this->uid)->order('create_time desc')
            ->paginate(10);
        $this->success('获取成功', $list);
    }

    //地址设置默认
    public function address_set_defalut(){
        $param = $this->request->param();
        if(empty($param['address_id'])){
            $this->error('未找到指定参数');
        }
        Db::name("address")->where("uid",$this->uid)->update(["is_default"=>0]);
        Db::name("address")->where("uid",$this->uid)->where("id",$param['address_id'])->update(["is_default"=>1]);
        $this->success('设置成功');
    }

    //添加地址
    public function add_address(){
        $param = $this->request->param();
        if(empty($param['username'])){
            $this->error('姓名不能为空');
        }
        if(empty($param['phone'])){
            $this->error('手机号不能为空');
        }
        if(empty($param['address_pca'])){
            $this->error('省市区地址不能为空');
        }
        if(empty($param['address_detailed'])){
            $this->error('详细地址不能为空');
        }
        $param["uid"] = $this->uid;
        $param["create_time"] = time();
        Db::name("address")->insert($param);
        $this->success('添加成功');
    }

    //编辑地址显示
    public function address_look(){
        $param = $this->request->param();
        if(empty($param['address_id'])){
            $this->error('未找到指定参数');
        }
        $list = Db::name("address")->where("id",$param['address_id'])->find();
        $this->success('获取成功', $list);
    }

    //编辑地址
    public function edit_address()
    {
        $param = $this->request->param();
        if(empty($param['address_id'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['username'])){
            $this->error('姓名不能为空');
        }
        if(empty($param['phone'])){
            $this->error('手机号不能为空');
        }
        if(empty($param['address_pca'])){
            $this->error('省市区地址不能为空');
        }
        if(empty($param['address_detailed'])){
            $this->error('详细地址不能为空');
        }
        $address_id = $param["address_id"];
        unset($param["address_id"]);
        Db::name("address")->where("id",$address_id)->update($param);
        $this->success('修改成功');
    }

    //删除地址
    public function del_address(){
        $param = $this->request->param();
        if(empty($param['address_id'])){
            $this->error('未找到指定参数');
        }
        Db::name("address")->where("id",$param['address_id'])->delete();
        $this->success('删除成功');
    }

    //我的默认地址
    public function mu_defa_address(){
        $list = Db::name("address")->where("uid",$this->uid)->where("is_default",1)->find();
        $this->success('获取成功', $list);
    }

    //个人数据
    public function personal_data(){
        $cou_num = Db::name("coupon_user")->where("uid",$this->uid)->where("status",1)->count();
        $colle_num = Db::name("my_collection")->where("uid",$this->uid)->count();
        $list = [
            "coupon_num"=>$cou_num,
            "collection_num"=>$colle_num
        ];
        $this->success('获取成功', $list);
    }

    //技师基础资料修改
    public function technician_data_modification(){
        $param = $this->request->param();
        if(empty($param['st_id'])){
            $this->error('未找到指定技师');
        }
        $st_id = $param["st_id"];
        unset($param["st_id"]);
        Db::name("service_technician")->where("id",$st_id)->update($param);
        $this->success('修改成功');
    }

     //上传图片
     public function upload()
     {
         $file = request()->file('file');
         if (empty($file)) {
             $this->error('请选择上传文件');
         }
         try {
             validate('app\api\validate\Upload')
                 ->scene('img')
                 ->check(['file' => $file]);
             $savename = \think\facade\Filesystem::disk('public')->putFile('uploads', $file);
             $url = request()->domain() . '/' . $savename;
             $this->success('上传成功',$url);
         } catch (\think\exception\ValidateException $e) {
             $this->error('上传失败',$e->getMessage());
         }
     }
     
     //技师修改密码
     public function tec_editpwd(){
        $param = $this->request->param();
        if(empty($param['st_id'])){
            $this->error('技师id不能为空');
        }
        if(empty($param['password'])){
            $this->error('新密码不能为空');
        }
        if(empty($param['new_password'])){
            $this->error('密码不能为空');
        }
        if($param['password'] != $param['new_password']){
            $this->error('两次密码不一致');
        }
        Db::name("service_technician")->where("id",$param['st_id'])->update(["password"=>md5($param['password'])]);
        $this->success('修改成功');
     }

     //修改用户信息
     public function modify_user_information(){
        $param = $this->request->param();
        if(empty($param['nickname'])){
            $this->error('昵称不能为空');
        }
        if(empty($param['avatar'])){
            $this->error('头像不能为空');
        }
        if(empty($param['gender'])){
            $this->error('性别不能为空');
        }
        Db::name("user")->where("id",$this->uid)->update($param);
        $this->success('修改成功');
     }

     //配置数据
     public function config_data(){
        $param = $this->request->param();
        if(empty($param['type'])){
            $this->error('类型不能为空');
        }
        if($param["type"] == 1){
            $service_agreement = sysconfig('contact','service_agreement');
            $service_agreement = html_entity_decode($service_agreement);
            $data = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $service_agreement);
        }elseif($param["type"] == 2){
            $data = sysconfig('site','contact_phone');
        }else{
            $this->error('类型不存在');
        }
        $this->success('获取成功', $data);
     }

     //分销商编辑显示
     public function distributor_edit_look(){
        $param = $this->request->param();
        if(empty($param['fid'])){
            $this->error('分销商id不能为空');
        }
        $list = Db::name("distributor")->where("id",$param["fid"])->find();
        $this->success('获取成功', $list);
     }

     //分销商编辑信息
     public function distributor_edit(){
        $param = $this->request->param();
        if(empty($param['fid'])){
            $this->error('分销商id不能为空');
        }
        if(empty($param['name'])){
            $this->error('姓名不能为空');
        }
        if(empty($param['age'])){
            $this->error('年龄不能为空');
        }
        if(empty($param['phone'])){
            $this->error('手机号不能为空');
        }
        if(empty($param['card'])){
            $this->error('身份证不能为空');
        }
        $data = [
            "name"=>$param["name"],
            "age"=>$param["age"],
            "phone"=>$param["phone"],
            "card"=>$param["card"],
        ];
        Db::name("distributor")->where("id",$param["fid"])->update($data);
        $this->success('编辑成功');
     }

     //判断是否是服务技师
     public function is_service_technician(){
        $flag = Db::name("service_technician")->where("uid",$this->uid)->find();
        if(empty($flag)){
            $this->error('不是');
        }else{
            $this->success('是',$flag);
        }
        
     }

     //服务技师申请
     public function technician_application(){
        $param = $this->request->param();
        if(empty($param['username'])){
            $this->error('姓名不能为空');
        }
        if(empty($param['img'])){
            $this->error('头像不能为空');
        }
        if(empty($param['age'])){
            $this->error('年龄不能为空');
        }
        if(empty($param['phone'])){
            $this->error('手机号不能为空');
        }
        if(empty($param['sex'])){
            $this->error('性别不能为空');
        }
        $lfag = Db::name("technician_review")->where("status",0)->where("uid",$this->uid)->find();
        if(!empty($lfag)){
            $this->error('已申请,请勿重复申请');
        }
        $flag = Db::name("technician_review")->where("status",2)->where("uid",$this->uid)->find();
        if(!empty($flag)){
            Db::name("technician_review")->where("status",2)->where("uid",$this->uid)->update(["type"=>1]);
        }
        $param["uid"] = $this->uid;
        $param["create_time"] = time();
        Db::name("technician_review")->insert($param);
        $this->success('提交成功');
     }

     //技师审核状态
     public function technician_audit_status(){
        $list = Db::name("technician_review")->where("uid",$this->uid)->where("type",0)->find();
        $this->success('获取成功',$list);
     }

     

    
 
   
}
